//
//  Boss.m
//  SpaceViking
//
//  Created by Long on 12/25/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "Boss.h"

@implementation Boss

@synthesize waitingAnim;
@synthesize slashingAnim;
@synthesize takingAHitAnim;
@synthesize dyingAnim;
@synthesize defendingAnim;
@synthesize slashingLeftAnim;
@synthesize slashingRepairAnim;
@synthesize slashingBeginAnim;
@synthesize slashingEndAnim;
@synthesize slashingLeftTopAnim;
@synthesize slashingRightTopAnim;
@synthesize slashingRightAnim;

- (void)dealloc {
    [waitingAnim release];
    [slashingAnim release];
    [takingAHitAnim release];
    [dyingAnim release];
    [defendingAnim release];
    [slashingLeftAnim release];
    [slashingLeftTopAnim release];
    [slashingRightTopAnim release];
    [super dealloc];
}

- (void)changeState:(CharacterStates)newState {
    [self stopAllActions];
    id action = nil;
    [self setCharacterState:newState];
    
    switch (newState) {
        case kStateDead:
            CCLOG(@"boss die");
            action = [CCAnimate actionWithAnimation:dyingAnim
                               restoreOriginalFrame:NO];
//            action = [[CCRepeatForever actionWithAction:[CCAnimate actionWithDuration:0.4f 
//                                                                                             animation:dyingAnim
//                                                                                  restoreOriginalFrame:YES]]retain];
            break;
            
        case kStateIdle:
            action = [CCAnimate actionWithAnimation:waitingAnim
                               restoreOriginalFrame:NO];
            break;
            
        case kStateSlash:
            action = [CCAnimate actionWithAnimation:slashingAnim 
                               restoreOriginalFrame:NO];
            break;
            
        case kStateTakingDamage:
            action = [CCAnimate actionWithAnimation:takingAHitAnim 
                               restoreOriginalFrame:NO];
            break;
            
        case kStateDefend:
            action = [CCAnimate actionWithAnimation:defendingAnim 
                               restoreOriginalFrame:NO];
            break;
        
        case kStateSlashLeft:
            action = [CCAnimate actionWithAnimation:slashingLeftAnim 
                               restoreOriginalFrame:NO];
            break;
            
        case kStateSlashRepair:
            action = [CCAnimate actionWithAnimation:slashingRepairAnim 
                               restoreOriginalFrame:NO];
            break;
            
        case kStateSlashBegin:
            action = [CCAnimate actionWithAnimation:slashingBeginAnim 
                               restoreOriginalFrame:NO];
            break;
            
        case kStateSlashEnd:
            action = [CCAnimate actionWithAnimation:slashingEndAnim 
                               restoreOriginalFrame:NO];
            break;
        case kStateSlashLeftTop:
            action = [CCAnimate actionWithAnimation:slashingLeftTopAnim 
                               restoreOriginalFrame:NO];
            break;
        case kStateSlashRightTop:
            action = [CCAnimate actionWithAnimation:slashingRightTopAnim 
                               restoreOriginalFrame:NO];
            break;
        case kStateSlashRight:
            action = [CCAnimate actionWithAnimation:slashingRightAnim 
                               restoreOriginalFrame:NO];
            break;

            
        default:
            break;
    }
    if(action != nil)
    {
        [self runAction:action];
    }
}

- (void)updateStateWithDeltaTime:(ccTime)deltaTime andListOfGameObjects:(CCArray *)listOfGameObjects {
    //timeBoss += deltaTime;
    
    //CCLOG(@"deltaTime = , %d",deltaTime);
    if(randomAction == 0 && characterState == kStateIdle) {
        [self changeState:kStateSlashRepair];
    }
    if(characterState == kStateSlashRepair && [self numberOfRunningActions] == 0) {
        [self changeState:kStateSlashBegin];
    }
    if(characterState == kStateSlashBegin && [self numberOfRunningActions] == 0) {
        [self changeState:kStateSlashEnd];
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
    
    if(randomAction == 1 && [self numberOfRunningActions] == 0){
        [self changeState:kStateSlashLeft];
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
    
    if(randomAction == 2 && [self numberOfRunningActions] == 0){
        [self changeState:kStateDefend];
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
    
    if(randomAction == 3 && [self numberOfRunningActions] == 0){
        [self changeState:kStateSlashLeftTop];
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
    
    if(randomAction == 4 && [self numberOfRunningActions] == 0){
        [self changeState:kStateSlashRightTop];
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
    
    if(randomAction == 5 && [self numberOfRunningActions] == 0){
        [self changeState:kStateSlashRight];
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
            
    if (([self numberOfRunningActions] == 0) && (characterState != kStateIdle) 
        && (characterState == kStateSlashEnd || characterState == kStateTakingDamage || characterState == kStateSlashLeft || characterState == kStateDefend || characterState == kStateSlashLeftTop || characterState == kStateSlashRightTop || characterState == kStateSlashRight)) {
        [self changeState:kStateIdle];   
        randomAction = arc4random_uniform(RANDOWM_ACTION_NUM);
    }
}

- (void)initAnimations {
    [self setWaitingAnim: [self loadPlistForAnimationWithName:@"waitingAnim" 
                                                 andClassName:NSStringFromClass([self class])]];
    [self setSlashingAnim: [self loadPlistForAnimationWithName:@"slashingAnim" 
                                                  andClassName:NSStringFromClass([self class])]];
    [self setTakingAHitAnim: [self loadPlistForAnimationWithName:@"takingAHitAnim" 
                                                 andClassName:NSStringFromClass([self class])]];
    [self setDyingAnim: [self loadPlistForAnimationWithName:@"dyingAnim" 
                                                  andClassName:NSStringFromClass([self class])]];
    [self setDefendingAnim: [self loadPlistForAnimationWithName:@"defendingAnim" 
                                                 andClassName:NSStringFromClass([self class])]];
    [self setSlashingLeftAnim: [self loadPlistForAnimationWithName:@"slashingLeftAnim" 
                                                    andClassName:NSStringFromClass([self class])]];
    [self setSlashingRepairAnim: [self loadPlistForAnimationWithName:@"slashingRepairAnim" 
                                                        andClassName:NSStringFromClass([self class])]];
    [self setSlashingBeginAnim: [self loadPlistForAnimationWithName:@"slashingBeginAnim" 
                                                        andClassName:NSStringFromClass([self class])]];
    [self setSlashingEndAnim: [self loadPlistForAnimationWithName:@"slashingEndAnim" 
                                                        andClassName:NSStringFromClass([self class])]];
    [self setSlashingLeftTopAnim: [self loadPlistForAnimationWithName:@"slashingLeftTopAnim" 
                                                     andClassName:NSStringFromClass([self class])]];
    [self setSlashingRightTopAnim: [self loadPlistForAnimationWithName:@"slashingRightTopAnim" 
                                                         andClassName:NSStringFromClass([self class])]];
    [self setSlashingRightAnim: [self loadPlistForAnimationWithName:@"slashingRightAnim" 
                                                          andClassName:NSStringFromClass([self class])]];
}

- (id)init {
    if((self = [super init])) {
        self.position = ccp(screenSize.width/2 , 0);
        self.anchorPoint = ccp(0.5,0);
        [self initAnimations];
        // khoi tao HP cho boss tai day
        characterHealth = 20;
        //[self setHealthOfPlayer:10];
        [self changeState:kStateIdle];
        randomAction = 0;
        //CCLOG(@"init randomAction = , %d",randomAction);
    }
    return self;
}

@end
